package com.g4.shoppingback.controller;

import com.g4.shoppingback.annotation.WriteLog;
import com.g4.shoppingback.common.PageVo;
import com.g4.shoppingback.common.Result;
import com.g4.shoppingback.pojo.DdOrderInfo;
import com.g4.shoppingback.service.DdOrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/shoppingback/ddOrderInfo")
public class DdOrderInfoController {

    @Autowired
    private DdOrderInfoService ddOrderInfoService;

    /**
     * 根据分页信息或订单名查询订单
     * @param pageVo 封装了页数和关键字
     * @return 返回分页信息
     */
    @PreAuthorize("hasAnyAuthority('miShop:order:find')")
    @GetMapping
    public Result selectOrderByPage(PageVo pageVo){
        return ddOrderInfoService.selectOrderByPage(pageVo);
    }

    /**
     * 根据id修改发货状态
     * @param ddOrderInfo 订单id和状态
     * @return 返回提示
     */
    @WriteLog("订单发货")
    @PreAuthorize("hasAnyAuthority('miShop:order:sendStatus')")
    @PutMapping("/{id}/{sendStatus}")
    public Result updateDelivery(DdOrderInfo ddOrderInfo){
        int row = ddOrderInfoService.updateDelivery(ddOrderInfo);
        if(row==1){
            return Result.success("发货成功");
        }return Result.error("发货失败");
    }

    /**
     * 根据id修改地址
     * @param ddOrderInfo 订单id和地址id
     * @return 返回提示
     */
    @WriteLog("修改订单发货地址")
    @PreAuthorize("hasAnyAuthority('miShop:order:updateAddress')")
    @PutMapping("/updateAddress/{id}/{addressId}")
    public Result updateAddressId(DdOrderInfo ddOrderInfo){
        int row = ddOrderInfoService.updateAddressId(ddOrderInfo);
        if(row==1){
            return Result.success("地址修改成功");
        }return Result.error("地址修改失败");
    }
}
